'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 神庙列表
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var dataApi = require('../../../../libs/dataApi');
var wishtreeConf = require('../../../../config/wishtreeConf');
var PlayerManager = require('../../../../libs/playerManager');
var Code = require('../../../../consts/code');
var buildType = require('../../../../consts/buildType');
var constantUtil = require('../../../../util/constantUtil');

module.exports = function (msg, session, next) {

    var role_id = session.uid;
    var free_wishtree_num;
    var wishtree_buy_list = [];
    var remainder_num = 0;
    var player_manager = new PlayerManager(role_id);

    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'wishtree', 'build', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var wishtree_model = player_manager.wishtree;
                var build_item = player_manager.build.getBuildByType(buildType.WISHTREE);
                var reward_list = _reward_buy_list(wishtree_model, build_item.lv);

                free_wishtree_num = wishtree_model.getFreeWishtreeNum();
                _.each(reward_list, function (reward_list_item) {
                    var value1 = _.clone(reward_list_item, true);
                    if (free_wishtree_num === 0) {
                        value1.diamond = wishtreeConf.getDiamondConsumptionConf(wishtree_model.getWishtreeNum(value1.reward_type) + 1);
                    }

                    if (value1.need_lvl <= build_item.lv) {
                        value1.state_value = 1;
                    } else {
                        value1.diamond = 0;
                        value1.state_value = 0;
                    }

                    wishtree_buy_list.push(value1);
                });
                remainder_num = constantUtil.value('wishingtree_time_limit') - wishtree_model.getWishtreeTotal();
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    free_wishtree_num: free_wishtree_num,
                    wishtree_buy_list: wishtree_buy_list,
                    remainder_num: remainder_num
                }
            });
        }
    );
};

var _reward_buy_list = function (wishtree_model, wishingtree_lvl) {
    var reward = {};
    var reward_buy_list = [];
    var rewardConf = dataApi.wishingtree_lvl.findById(wishingtree_lvl);
    reward = _.clone(rewardConf, true);
    if (!reward) {
        return 0;
    } else {
        _.each(reward, function (reward_item) {
            var reward_item1 = _.clone(reward_item, true);
            reward_item1.reward_num = Math.ceil(reward_item1.reward_num * _effWishtree(wishtree_model.getWishtreeNum(reward_item1.reward_type) + 1));
            reward = reward_item1;
            reward_buy_list.push(reward);
        });
        return reward_buy_list;
    }
};

// 神庙增益百分比
var _effWishtree = function (num) {
    if (num > 0) {
        var eff_value = dataApi.wishingtree_effect.findById(num).value;
        return eff_value / 100;
    } else {
        return 1;
    }
};
